<?php
$blog='NULL';
$blogdata='NULL';
$optiondomain='system';
require('private.php');
require(CONFIGPATH . 'wp-config.php');

$action = $_REQUEST['action'];
$error = '';

nocache_headers();

switch($action) {

case 'lostpassword':
do_action('lost_password');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>Lyceum blog &raquo; <?php _e('Lost Password') ?></title>
	<meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" />
	<link rel="stylesheet" href="<?php echo WEBROOT ?>/wp-admin/wp-admin.css" type="text/css" />
	<script type="text/javascript">
	function focusit() {
		// focus on first input field
		document.getElementById('user_login').focus();
	}
	window.onload = focusit;
	</script>
	<style type="text/css">
	#user_login, #email, #submit {
		font-size: 1.7em;
	}
	</style>
</head>
<body>
<div id="login">
<h1><a href="http://lyceum.ibiblio.org/">Lyceum</a></h1>
<?php if ( isset($_SESSION['error']) ) :?>
	<div class="error">
	<ul>
	<?php
	echo "<li>".$_SESSION['error']."</li>";
	?>
	</ul>
	</div>
<?php unset($_SESSION['error']); endif;
	if ( isset($_SESSION['success']) ) :?>
	<div class="updated">
	<ul>
	<?php
	echo "<li>".$_SESSION['success']."</li>";
	?>
	</ul>
	</div>
<?php unset($_SESSION['success']); endif; ?> 
<p><?php _e('Please enter your information here. You will be sent an email with instructions for changing your password.') ?></p>

<form name="lostpass" action="<?php echo WEBROOT ?>/lost-pass.php" method="post" id="lostpass">
<p>
<input type="hidden" name="action" value="sendemail" />
<label><?php _e('Username:') ?><br />
<input type="text" name="user_login" id="user_login" value="" size="20" tabindex="1" /></label></p>
<p><label><?php _e('E-mail:') ?><br />
<input type="text" name="email" id="email" value="" size="25" tabindex="2" /></label><br />
</p>
<p class="submit"><input type="submit" name="submit" id="submit" value="<?php _e('Reset Password'); ?> &raquo;" tabindex="3" /></p>
</form>
<ul>
	<li><a href="<?php echo LURL; ?>/" title="<?php _e('Are you lost?') ?>">&laquo; <?php _e('Back to front page') ?></a></li>
<?php if (get_settings('users_can_register')) : ?>
	<li><a href="<?php echo LURL; ?>/register"><?php _e('Register') ?></a></li>
<?php endif; ?>
	<li><a href="<?php echo LURL; ?>/login"><?php _e('Login') ?></a></li>
</ul>
</div>
</body>
</html>
<?php
break;

case 'sendemail':
	$user_data = get_userdatabylogin($_POST['user_login']);
	// redefining user_login ensures we return the right case in the email
	$user_login = $user_data->user_login;
	$user_email = $user_data->user_email;

	if (!$user_email || $user_email != $_POST['email'])
	{
		$_SESSION['error'] = '<strong>ERROR</strong>: Sorry, that username / email combination does not exist in the database.';
		wp_redirect($_SERVER['HTTP_REFERER']);
		break;
	}
do_action('retreive_password', $user_login);  // Misspelled and deprecated.
do_action('retrieve_password', $user_login);

	// Generate something random for a password... md5'ing current time with a rand salt
	$key = substr( md5( uniqid( microtime() ) ), 0, 50);
	// now insert the new pass md5'd into the db
 	$wpdb->query("UPDATE $wpdb->users SET user_activation_key = '$key' WHERE user_login = '".mysql_real_escape_string($user_login)."'");
	$message = __('Someone has asked to reset the password for the following site and username.') . "\r\n\r\n";
	$message .= LURL . "\r\n\r\n";
	$message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n";
	$message .= __('To reset your password visit the following address, otherwise just ignore this email and nothing will happen.') . "\r\n\r\n";
	$message .= LURL . "/lost-pass.php?action=inputpass&key=$key\r\n";

	$m = wp_mail($user_email, sprintf(__('[%s] Password Reset'), get_settings('site_title')), $message);

	if ($m == false) 
		$_SESSION['error'] = '<strong>ERROR</strong>: The e-mail could not be sent. Possible reason: your host may have disabled the mail() function...<br/>';
	else 
		$_SESSION['success'] = "The e-mail was sent successfully to $user_login's e-mail address.";
	wp_redirect($_SERVER['HTTP_REFERER']);
break;

case 'inputpass' :

	// Generate something random for a password... md5'ing current time with a rand salt
	$key = preg_replace('/a-z0-9/i', '', $_GET['key']);
	$user = $wpdb->get_row("SELECT * FROM $wpdb->users WHERE user_activation_key = '$key'");
	if ( empty($key) || !$user)
	{
		$_SESSION['error'] = '<strong>ERROR</strong>: Sorry, that unique key does not appear to be valid.';
		wp_redirect(LURL.'/lost-pass.php?action=lostpassword');
		break;
	}?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>Lyceum blog &raquo; <?php _e('Change Password') ?></title>
	<meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" />
	<link rel="stylesheet" href="<?php echo WEBROOT ?>/wp-admin/wp-admin.css" type="text/css" />
	<script type="text/javascript">
	function focusit() {
		// focus on first input field
		document.getElementById('password1').focus();
	}
	window.onload = focusit;
	</script>
	<style type="text/css">
	#password1, #password2, #submit {
		font-size: 1.7em;
	}
	</style>
</head>
<body>
<div id="login">
<h1><a href="http://lyceum.ibiblio.org/">Lyceum</a></h1>
<?php if ( isset($_SESSION['error']) ) :?>
	<div class="error">
	<ul>
	<?php
	echo "<li>".$_SESSION['error']."</li>";
	?>
	</ul>
	</div>
<?php unset($_SESSION['error']); endif;?>
<p><?php _e('Enter your desired password twice. If the password change is successful, you will be automatically logged in.') ?></p>

<form name="changepass" action="<?php echo WEBROOT ?>/lost-pass.php" method="post" id="updatepass">
<p>
<input type="hidden" name="action" value="updatepass" />
<input type="hidden" name="key" value="<?php echo $key?>" />
<label><?php _e('New Password:') ?><br />
<input type="password" name="password1" id="password1" value="" size="20" tabindex="1" /></label></p>
<p><label><?php _e('Confirm New Password:') ?><br />
<input type="password" name="password2" id="password2" value="" size="20" tabindex="1" /></label><br />
</p>
<p class="submit"><input type="submit" name="submit" id="submit" value="<?php _e('Change Password'); ?> &raquo;" tabindex="3" /></p>
</form>
<ul>
	<li><a href="<?php echo LURL; ?>/" title="<?php _e('Are you lost?') ?>">&laquo; <?php _e('Back to front page') ?></a></li>
<?php if (get_settings('users_can_register')) : ?>
	<li><a href="<?php echo LURL; ?>/register"><?php _e('Register') ?></a></li>
<?php endif; ?>
	<li><a href="<?php echo LURL; ?>/login"><?php _e('Login') ?></a></li>
</ul>
</div>
</body>
</html>
<?php
break;

case 'updatepass' :
	$pass1 = $_POST['password1'];
	$pass2 = $_POST['password2'];
	$key = $_POST['key'];
	$user = $wpdb->get_row("SELECT * FROM $wpdb->users WHERE user_activation_key = '$key'");

	if ( empty($key) || !$user)
	{
		$_SESSION['error'] = '<strong>ERROR</strong>: Sorry, that unique key does not appear to be valid.';
		wp_redirect(LURL.'lost-pass.php?action=lostpassword');
		break;
	}
	if ($pass1 == '' || $pass2 == '')
		$_SESSION['error'] = __('<strong>ERROR</strong>: Please enter your password twice.');
	/* Check for "\" in password */
	if( strpos( " ".$pass1, "\\" ) )
		$_SESSION['error'] = __('<strong>ERROR</strong>: Passwords may not contain the character "\\".');
	/* checking the password has been typed twice the same */
	if (strcmp($pass1, $pass2) != 0)
		$_SESSION['error'] = __('<strong>ERROR</strong>: Please type the same password in the two password fields.');

	if (isset($_SESSION['error']))
	{
		wp_redirect($_SERVER['HTTP_REFERER']);
		break;
	}
	$wpdb->query("UPDATE $wpdb->users SET user_activation_key = '', user_pass = '" . md5($pass1) . "' WHERE user_login = '$user->user_login'");

	// login user automatically if password change has been successful
	$user_login = $user->user_login;
	$user_pass  = $pass1;
	$rememberme = false;

	do_action('wp_authenticate', array(&$user_login, &$user_pass));

	$user = new WP_User(0, $user_login);
	wp_setcookie($user_login, $user_pass, false, '', '', $rememberme);
	do_action('wp_login', $user_login);
	wp_redirect(LURL . '/profile');
break;
}
?>
